//
//  PushManager.m
//  AppointmentForCar
//
//  Created by TheMacBook on 2016/11/9.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "PushManager.h"
#import "CallCarModel.h"
@interface PushManager () <GeTuiSdkDelegate>


@end
@implementation PushManager
static PushManager * manager;

+ (instancetype)sharePushManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PushManager alloc] init];
    });
    return manager;
}
- (instancetype)init
{
    self = [super init];
    if(self)
    {
        [GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];
        [self.class registerUserNotification];
    }
    return self;
}
#pragma mark - 用户通知(推送) _自定义方法

/** 注册用户通知 */
+ (void)registerUserNotification {
    
    /*
     注册通知(推送)
     申请App需要接受来自服务商提供推送消息
     */
    
    // 判读系统版本是否是“iOS 8.0”以上
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ||
        [UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
        
        // 定义用户通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音)
        UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
        
        // 定义用户通知设置
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        
        // 注册用户通知 - 根据用户通知设置
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    } else { // iOS8.0 以前远程推送设置方式
        // 定义远程通知类型(Remote.远程 - Badge.标记 Alert.提示 Sound.声音)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
        // 注册远程通知 -根据远程通知类型
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
#pragma clang diagnostic pop

    }
}
/**
 *  判断pushSDK是否注册成功
 */
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
    
    NSLog(@"[GeTuiSdk RegisterClient]:%@", clientId);
    [GeTuiSdk setPushModeForOff:!_pushSwith];
}



- (void)setCid:(NSString *)cid
{
    _cid = cid;
    if(_block)
    {
        _block(cid);
    }
}
- (void)getClientID:(clientIdBlock)block
{
    if(_cid)
    {
        block(_cid);
    }else
    {
        _block = block;
    }
    
}
- (void)getCallPassengerInfo:(callPassengerInfo)block {
    _orderInfoBackCall = block;
}

- (void)getPushInfo:(NSDictionary *)pushInfo {
    NSDictionary * dic = pushInfo[@"payload"];
    CallCarModel * model = [CallCarModel mj_objectWithKeyValues:dic];
    if(_orderInfoBackCall) {
        _orderInfoBackCall(model);
    }
}
#pragma mark - setting and getting
- (void)setPushSwith:(BOOL)pushSwith
{
    _pushSwith = pushSwith;
    [GeTuiSdk setPushModeForOff:!pushSwith];
}


@end
